#include "executable_file_analyzer.h"

#include "posix/elf_parser.h"
#include "windows/pe_parser.h"

namespace analyzer
{

namespace posix
{

} // namespace posix

executable_file_analyzer::executable_file_analyzer(std::filesystem::path file_path) noexcept : path(std::move(file_path))
{
    parsers.push_back(std::make_unique<posix::elf_parser>(path));
    parsers.push_back(std::make_unique<windows::pe_parser>(path));
}

file_structure executable_file_analyzer::analyze()
{
    if (!std::filesystem::exists(path))
        throw std::runtime_error("file not exists");

    std::optional<file_structure> file_struct;
    for (size_t i = 0; i < parsers.size() && !file_struct; ++i)
        file_struct = parsers[i]->parse();

    if (!file_struct)
        throw std::runtime_error("unknown format");

    return *file_struct;
}

} // namespace analyzer
